package com.rock.code.province.hn.coll;

import com.rock.code.common.util.SnmpClient;
import com.rock.code.common.util.SpringContextUtil;
import com.rock.code.common.util.WriteFile;
import com.rock.code.common.util.WriteKPI;
import com.rock.code.datacollconfig.coll.interfaces.CollDataItemInterface;
import com.rock.code.datacollconfig.gap.dao.TbCollConfSnmpDao;
import com.rock.code.datacollconfig.gap.domain.TbCollConfSnmp;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class GAPCollImpl implements CollDataItemInterface {

    @Resource
    TbCollConfSnmpDao tbCollConfSnmpDao;

    @Override
    public void execute(String[] args) throws Exception {
        System.out.println("============GAP start============");
        tbCollConfSnmpDao = (TbCollConfSnmpDao) SpringContextUtil.getBeanByClass(TbCollConfSnmpDao.class);

        HashMap pramMap = new HashMap();
        pramMap.put("enable", "1");
        pramMap.put("dev_type", "1");
        List<TbCollConfSnmp> _list = tbCollConfSnmpDao.getAllCollSnmpConf(pramMap);
        System.out.println("_list的大小：" + _list.size());
        //snmpclient 放入缓存
        HashMap<String, SnmpClient> cacheMap = new HashMap<>();
        for (TbCollConfSnmp tbCollConfSnmp : _list) {
            String snmp_ip = tbCollConfSnmp.getSnmp_ip();
            if (cacheMap.get(snmp_ip) == null || cacheMap.isEmpty()) {
                cacheMap.put(tbCollConfSnmp.getSnmp_ip(), getSnmpClient(tbCollConfSnmp));
            }
        }

        for (TbCollConfSnmp tbCollConfSnmp : _list) {
            //采集snmp的指标信息
            System.out.println(tbCollConfSnmp.getSnmp_name());
            String ip = tbCollConfSnmp.getSnmp_ip();
            // snmp 的snmpclient
            SnmpClient snmpClient = getSnmpClient(tbCollConfSnmp);
            if (snmpClient == null) {
                System.out.println("不能获得SnmpClient");
                continue;
            }
            String kbp = tbCollConfSnmp.getKbp();
            String path = tbCollConfSnmp.getPath();

            doGAPData(tbCollConfSnmp, snmpClient);
        }
        System.out.println("============GAP end============");
    }

    private SnmpClient getSnmpClient(TbCollConfSnmp tbCollConfSnmp) {
        String ip = tbCollConfSnmp.getSnmp_ip();
        String community = tbCollConfSnmp.getCommunity();
        String snmp_version = tbCollConfSnmp.getSnmp_version();
        String securitylevel = tbCollConfSnmp.getSecuritylevel();
        String port = tbCollConfSnmp.getSnmp_port();
        String user = tbCollConfSnmp.getSnmp_user();
        String pwd = tbCollConfSnmp.getSnmp_pwd();
        String method = tbCollConfSnmp.getMethod();
        String protocol = tbCollConfSnmp.getProtocol();
        String passphrase = tbCollConfSnmp.getPassphrase();
        SnmpClient snmp = null;
        if ("v2c".equals(snmp_version)) {
            snmp = SnmpClient.create("v2c")
                    .setHost(ip)
                    .setPort(port)
                    .setCommunity(community);
        } else if ("3".equals(snmp_version)) {

            if ("authNoPriv".equals(securitylevel)) {
                snmp = SnmpClient.create("3")
                        .setHost(ip)
                        .setPort(port)
                        .setSerurityName(user)
                        .setAuthenticationProtocol(protocol)
                        .setAuthenticationPassphrase(passphrase)
                        .setSecurityLevel(securitylevel);
            } else if ("authPriv".equals(securitylevel)) {
                snmp = SnmpClient.create("3")
                        .setHost(ip)
                        .setPort(port)
                        .setSerurityName(user)
                        .setAuthenticationProtocol(protocol)
                        .setAuthenticationPassphrase(pwd)
                        .setPrivacyProtocol(protocol)
                        .setPrivacyPassphrase(passphrase)
                        .setSecurityLevel(securitylevel);
            } else if ("noAuthNoPriv".equals(securitylevel)) {
                snmp = SnmpClient.create("3")
                        .setHost(ip)
                        .setPort(port)
                        .setSerurityName(user)
                        .setSecurityLevel(securitylevel);
            }
        }
        return snmp;
    }

    private void doGAPData(TbCollConfSnmp tbCollConfSnmp, SnmpClient snmpClient) {
        if (snmpClient == null) {
            return;
        }
        WriteKPI writeKPI = new WriteKPI();
        String dataTime = WriteKPI.getNowTime();
        String kpi_id = tbCollConfSnmp.getKpi_id();
        String kbp = tbCollConfSnmp.getKbp();
        String path = tbCollConfSnmp.getPath();
        String oid = tbCollConfSnmp.getSnmp_oid();
        String resource_type = tbCollConfSnmp.getResource_type();

        String ip = tbCollConfSnmp.getSnmp_ip();
        String unit_id = kbp + ":" + ip.replace(".", "_");
        String kpi_value = null;

        try {
            snmpClient.connect();
            Map<String, String> result = snmpClient.walk(oid);
            System.out.println(result);
            int size = result.size();
            if (size > 1) {
                long sum = 0;
                for (String value : result.values()) {
                    sum += Long.parseLong(value);
                }
                kpi_value = String.valueOf(sum / size);
            }else{
                kpi_value = result.values().iterator().next();
                kpi_value = Objects.equals(kpi_value, "") ?"null":kpi_value;
            }
            snmpClient.close();
        } catch (Exception e) {
            e.printStackTrace();
            kpi_value = "null";
        }

        // CPU
        if (oid.startsWith("1.3.6.1.4.1.9833.1.4.1.1.4")) {
            unit_id += "-cpu";
        }
        // 内存
        else if (oid.startsWith("1.3.6.1.4.1.9833.1.4.2.11")) {
            unit_id += "-mem";
        } else {
            unit_id += "-total";
        }

        // 空闲CPU
        if ("1.3.6.1.4.1.9833.1.4.1.1.4".equals(oid)) {
            kpi_value = String.valueOf(100 - Integer.parseInt(kpi_value));
        }

        // 空闲内存
        if ("1.3.6.1.4.1.9833.1.4.2.11".equals(oid)) {
            kpi_value = String.valueOf((long) (Double.parseDouble(kpi_value)/3281956*100));
        }
    
        switch (oid) {
            case "1.3.6.1.4.1.9833.1.4.1.1.2":
            case "1.3.6.1.4.1.9833.1.4.1.1.3":
            case "1.3.6.1.4.1.9833.1.4.3.1.9":
            case "1.3.6.1.4.1.9833.1.4.3.1.10":
                kpi_value += "%";
                break;
            case "1.3.6.1.4.1.9833.1.4.1.1.5":
            case "1.3.6.1.4.1.9833.1.4.1.1.6":
            case "1.3.6.1.4.1.9833.1.4.1.1.7":
            case "1.3.6.1.4.1.9833.1.4.1.1.8":
            case "1.3.6.1.4.1.9833.1.4.1.1.9":
            case "1.3.6.1.4.1.9833.1.4.1.1.10":
            case "1.3.6.1.4.1.9833.1.4.1.1.11":
                long time = Long.parseLong(kpi_value) / 100;
                kpi_value = time / (3600 * 24) + "day, "
                        + (time % (3600 * 24)) / 3600 + "hour, "
                        + (time % (3600 * 24) % 3600) / 60 + "minute, "
                        + time % (3600 * 24) % 3600 % 60 + "second ";
                break;
            case "1.3.6.1.4.1.9833.1.4.2.3":
            case "1.3.6.1.4.1.9833.1.4.2.4":
            case "1.3.6.1.4.1.9833.1.4.2.5":
            case "1.3.6.1.4.1.9833.1.4.2.6":
            case "1.3.6.1.4.1.9833.1.4.2.7":
            case "1.3.6.1.4.1.9833.1.4.2.8":
            case "1.3.6.1.4.1.9833.1.4.2.9":
            case "1.3.6.1.4.1.9833.1.4.2.10":
            case "1.3.6.1.4.1.9833.1.4.2.12":
            case "1.3.6.1.4.1.9833.1.4.2.13":
            case "1.3.6.1.4.1.9833.1.4.2.14":
            case "1.3.6.1.4.1.9833.1.4.2.15":
            case "1.3.6.1.4.1.9833.1.4.3.1.4":
            case "1.3.6.1.4.1.9833.1.4.3.1.5":
            case "1.3.6.1.4.1.9833.1.4.3.1.6":
            case "1.3.6.1.4.1.9833.1.4.3.1.7":
            case "1.3.6.1.4.1.9833.1.4.3.1.8":
                kpi_value = String.format("%.0f", Long.valueOf(kpi_value).doubleValue() / 1024) + " MB";
                break;
            default:
                break;
        }

        System.out.println(kpi_value);
        writeKPI.writeKPItoList(kpi_value, kpi_id, unit_id, dataTime, resource_type);
        WriteFile wf = new WriteFile();
        wf.writeSevenStrucFile(path, writeKPI.getVECTOR());
    }
    
    public static void main(String[] args) {
        try {
            SnmpClient snmp = SnmpClient.create("v2c").setHost("20.58.21.142").setPort("161").setCommunity("public");
            System.out.println(snmp);
            snmp.connect();
            Map<String, String> result = snmp.walk("1.3.6.1.2.1.1");
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
